// jqgrid表格配置
    // 使jqgrid的宽和高自适应父元素
    // $(function(){ 
    //     pageInit();
    // });
    // $(document).ready(function(){
    //     pageInit();
    // });  
    // 初始化jqGrid
    // 状态显示
    function subTaskState(subTaskState){
        if(subTaskState == 0){
            return '未处理';
        }else if(subTaskState == 1) {
            return '未审核';
        }else if(subTaskState == 2) {
            return '已完成';
        }else if(subTaskState == 3) {
            return '待整改';
        }
    }
    // 操作按钮
    function check(subTaskState) {
        var check = "";
        if(subTaskState == 0 || subTaskState == 3){
            check = "<a href='#qualitycontrol-assignmentGroup-handle' class='btn btn-primary btn-sm  dispose handle' title='处理' >处理</a>";
        }else if(subTaskState == 1){
            check = "<a href='#qualitycontrol-assignmentGroup-check' class='btn btn-primary btn-sm check' title='审核'>审核</a>";
        }
        return check;
    };
    function pageInit(){
        var parentHeight,parentWidth;
        parentHeight = $("#pending-table").height()*1 - 23,
        parentWidth = $("#pending-table").width()*1 + 14;
        //创建jqGrid组件
        jQuery("#pendingList").jqGrid({
            url: contextPath + "/subtask/queryTodoSubTaskJqg",
            datatype: "json",
            height: parentHeight,
            width: parentWidth,
            colNames:['子任务Id','任务名称','设备编号','设备类型Id','设备类型','工单类型Id','工单类型','施工单位','项目名称','产品线','状态','派单时间','备注信息','操作'],
            colModel:[
                    {name:'subTaskId',index:'subTaskId', sortable: false,hidden: true},
                    {name:'subTaskName',index:'subTaskName', sortable: false},
                    {name:'deviceId',index:'deviceId', sortable: false},
                    {name:'deviceTypeId',index:'deviceTypeId', sortable: false,hidden: true},
                    {name:'deviceTypeName',index:'deviceTypeName', sortable: false},
                    {name:'workOrderTypeId',index:'workOrderTypeId', sortable: false,hidden: true},
                    {name:'workOrderTypeName',index:'workOrderTypeName', sortable: false},
                    {name:'organizationTeamName',index:'organizationTeamName', sortable: false},
                    {name:'projectName',index:'projectName', sortable: false},
                    {name:'productLineName',index:'productLineName', sortable: false},
                    {name:'subTaskState',index:'subTaskState', sortable: false,formatter: subTaskState},
                    {name:'assignTaskTime',index:'assignTaskTime', sortable: false},
                    {name:'description',index:'description', sortable: false},
                    {name:'subTaskState',index:'subTaskState', sortable: false,formatter: check}
            ],
            sortname:'id',
            sortorder:'desc',
            viewrecords: true,
            rowNum: 20,
            rowList:[20,50,100],
            pager:"#gridpager"
        });
        /*创建jqGrid的操作按钮容器*/
        jQuery("#pendingList").jqGrid('navGrid', '#gridpager', {edit: false,add: false, del:false,search: false});
    }; 
    //函数：获取尺寸
    var timer = 0;
    $(window).resize(function(){
        clearTimeout(timer);
        timer = setTimeout(function() {
            $("#pendingList").setGridWidth($('#pending-table').width() + 12);
            $("#pendingList").setGridHeight($('#pending-table').height() - 23);
        }, 260);
    });
    // 侧边栏缩小时，实时的给jqgrid的宽和高赋值
    $("#asidebarSwitch").on('click',function() {
        var time = 0;
        var set = setInterval(function (){
            $("#pendingList").setGridWidth($('#pending-table').width()*1 + 14);　　
            $("#pendingList").setGridHeight($('#pending-table').height()*1 - 23);
            time++;
            if(time == 201){
                clearInterval(set);
            }
        },time);
    });

// 查询区域
    // 查询所有产品线
    $.ajax({
        url: contextPath + "/ProductLine/queryAllProductLine",
        type: 'GET',
        dataType: "json",
        contentType: "application/json",
        success: function(data){
            var str = '<option value="" selected>选择产品线</option>';
            for (var i =0 ,len = data.length; i < len; i++) {
            	str += '<option value="' + data[i].id + '">' + data[i].name + "</option>";
            }
            $("#productLineId").html(str);
        },
        error: function(data){
            toastr.error(data.message, "请求失败");
        }
    });
    // 查询设备类型
    $.ajax({
        url: contextPath + "/DeviceType/queryAllDeviceType",
        type: 'GET',
        dataType: "json",
        contentType: "application/json",
        success: function(data){
            var str = '<option value="" selected>选择设备类型</option>';
            for (var i =0 ,len = data.length; i < len; i++) {
            	str += '<option value="' + data[i].id + '">' + data[i].name + "</option>";
            }
            $("#deviceTypeId").html(str);
        },
        error: function(data){
            toastr.error(data.message, "请求失败");
        }
    });
    // 查询工单类型
    $.ajax({
        url: contextPath + "/WorkOrderType/queryAllWorkOrderType",
        type: 'GET',
        dataType: "json",
        contentType: "application/json",
        success: function(data){
            var str = '<option value="" selected>选择工单类型</option>';
            for (var i =0 ,len = data.length; i < len; i++) {
            	str += '<option value="' + data[i].id + '">' + data[i].name + "</option>";
            }
            $("#workOrderTypeId").html(str);
        },
        error: function(data){
            toastr.error(data.message, "请求失败");
        }
    });
    // 查询
    $('#query').on('click',function(){
        var productLineId = $("#productLineId").val();
        var projectName = $("#projectName").val();
        var constructionTeamName = $("#constructionTeamName").val();
        var deviceName = $("#deviceName").val();
        var deviceTypeId = $("#deviceTypeId").val();
        var workOrderTypeId = $("#workOrderTypeId").val();
        var subTaskState = $("#subTaskState").val();
        $("#pendingList").setGridParam({
            url : contextPath + "/subtask/queryTodoSubTaskJqg",
            postData : {
                queryCond: {
                    productLineId: productLineId,
                    projectName: projectName,
                    constructionTeamName: constructionTeamName,
                    deviceName: deviceName,
                    deviceTypeId: deviceTypeId,
                    workOrderTypeId: workOrderTypeId,
                    subTaskState: subTaskState
                }
            },
            page: 1
        }).trigger("reloadGrid");
    });

    // 审核
    // 获取子任务数据
    $("#pendingList").on('click','.check,.dispose',function(){
        var id = $(this).parent().parent().attr('id');
        var subTaskData = $("#pendingList").jqGrid('getRowData',id);
        console.log(subTaskData.subTaskId);
        $.cookie('subTaskId',JSON.stringify(subTaskData.subTaskId));
    });